package com.marketing.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.marketing.service.AdvertisePositionService;
import com.nameless.entity.Advertise;
import com.nameless.entity.AdvertisePosition;
import com.nameless.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 广告位置控制器
 * -*- coding:utf-8 -*-
 *
 * @author 太阳偷渡青山
 * @software IntelliJ IDEA
 * @date 2023/02/28 17:31
 * @Description
 */
@RestController
@Api("广告位置api信息操作接口")
@Slf4j
@RequestMapping("/marketing/api/advertisePosition")
public class AdvertisePositionController {

    @Autowired
    private AdvertisePositionService advertisePositionService;

    @PostMapping("{current}/{pageSize}")
    @ApiOperation(value = "分页查询信息并携带查询条件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页码，默认为1",example = "1",
                    paramType = "path",dataType = "int",required = false),
            @ApiImplicitParam(name = "pageSize", value = "每页显示条数,默认5", example = "5",
                    paramType = "path", dataType = "int", required = false)
    })
    public Result<PageInfo<AdvertisePosition>> findPageCustomer(@PathVariable Integer current, @PathVariable Integer pageSize,
                                                            @RequestBody(required = false) AdvertisePosition searchObj){


        PageHelper.startPage(current,pageSize);

        // 构造一个分页器，传递当前页与每页的页数
        Page<AdvertisePosition> setPage = new Page<>(current, pageSize);

        LambdaQueryWrapper<AdvertisePosition> wrapper = new LambdaQueryWrapper<>();

        wrapper.like(StringUtils.isNotEmpty(searchObj.getName()),AdvertisePosition::getName,searchObj.getName());
        List<AdvertisePosition> list = advertisePositionService.list(wrapper);

        PageInfo<AdvertisePosition> objectPageInfo1 = new PageInfo<>(list);

//        Page<AdvertisePosition> page = advertisePositionService.page(setPage, wrapper);
        return Result.success(objectPageInfo1);
    }

    @GetMapping("{id}")
    @ApiOperation(value = "根据广告位置编号信息")
    public Result<AdvertisePosition> getOne(@PathVariable Long id){
        AdvertisePosition byId = advertisePositionService.getById(id);
        return Result.success("id为："+id+"的优惠劵查询成功",byId);
    }

    @DeleteMapping
    @ApiOperation(value = "删除多个广告位置")
    @ApiImplicitParam(name = "idList",value = "需要删除的医院配置id集合")
    public Result<String> delete(@RequestBody List<Integer> idList){
        log.info("idList = {}",idList);
        boolean b = advertisePositionService.removeByIds(idList);
        return b?Result.success("删除成功"):Result.error("删除失败");
    }

    @PostMapping
    @ApiOperation(value = "添加广告位置信息")
    public Result<String> addCustomer(@RequestBody AdvertisePosition advertisePosition){
        boolean save = advertisePositionService.save(advertisePosition);
        return save?Result.success("添加成功"):Result.error("添加失败");
    }

    @PutMapping
    @ApiOperation(value = "修改广告位置信息")
    public Result<String> editCustomer(@RequestBody AdvertisePosition advertisePosition){
        boolean save = advertisePositionService.updateById(advertisePosition);
        return save?Result.success("修改成功"):Result.error("修改失败");
    }
}
